package com.example.fresh_market.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.example.fresh_market.constants.StrConstant;
import com.example.fresh_market.databinding.ActivityAddressEditBinding;
import com.example.fresh_market.entity.Address;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 编辑地址
 */
public class AddressEditActivity extends AppCompatActivity {
    private ActivityAddressEditBinding binding;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        binding = ActivityAddressEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        String json = getIntent().getStringExtra("address");
        if (StringUtils.isNotBlank(json)) {
            Address address = new Gson().fromJson(json, Address.class);
            binding.editId.setText(String.valueOf(address.getId()));
            binding.editRealName.setText(address.getRealName());
            binding.editPhone.setText(address.getPhone());
            binding.editProvince.setText(address.getProvince());
            binding.editCity.setText(address.getCity());
            binding.editDistrict.setText(address.getDistrict());
            binding.editDetail.setText(address.getDetail());
            binding.editDefault.setChecked(address.getIsDefault());
        }
        SharedPreferences preferences = getSharedPreferences(StrConstant.CONFIG, MODE_PRIVATE);
        String token = preferences.getString(StrConstant.TOKEN, "");
        binding.editSubmit.setOnClickListener(v -> {
            Map<String, String> params = Map.of(
                    "id", binding.editId.getText().toString(),
                    "realName", binding.editRealName.getText().toString(),
                    "phone", binding.editPhone.getText().toString(),
                    "province", binding.editProvince.getText().toString(),
                    "city", binding.editCity.getText().toString(),
                    "district", binding.editDistrict.getText().toString(),
                    "detail", binding.editDetail.getText().toString(),
                    "isDefault", binding.editDefault.isChecked() ? "true" : "false"
            );
            if (StringUtils.isBlank(json)) {
                HttpUtil.post(mContext, "/usr/address", Map.of(HttpUtil.Auth, token), params, response -> {
                    ToastUtil.showToast(this, "添加成功!");
                    finish();
                });
            } else {
                HttpUtil.put(mContext, "/usr/address", Map.of(HttpUtil.Auth, token), params, response -> {
                    ToastUtil.showToast(this, "更新成功!");
                    finish();
                });
            }
        });
    }
}
